home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 11.8 KB | 467 lines |
- /*
- Copyright (c) 1995, 1996 Connect! Corporation, Inc. All Rights Reserved.
- Source code usage restricted as defined in Connect! Widgets License Agreement
- */
-
- package symantec.itools.awt;
-
- import java.awt.*;
- import java.util.Vector;
- import java.lang.Boolean;
-
- // 01/29/97 RKM Integrated Tim's changes to preserve the current index
-
- /**
- * TabPanel is a Panel extension which provides for
- * a tabbed dialog effect. It will automatically manage
- * swapping panels when a tab is shown/activated.
- * It can be used directly or extended.
- * When extending from TabPanel be sure to super()
- * during construction and to super.handleEvent(evt) from
- * handleEvent if you override it.
- *
- * @author Scott Fauerbach
- */
- public class TabPanel extends BaseTabbedPanel
- {
- Vector vPanels;
-
- String[] labels=null;
-
- boolean bOsHack;
-
- /**
- * Constructs a TabPanel with tabs on top, rounded
- */
- public TabPanel()
- {
- this(TOP, ROUNDED);
- }
-
- /**
- * Obsolete. Use public TabPanel(int tabsPostion, int tabsStyle)
- * Constructs a TabPanel.
- */
- public TabPanel(boolean bTabsOnTop)
- {
- this(bTabsOnTop ? TOP : BOTTOM, bTabsOnTop ? ROUNDED : SQUARE);
- }
-
- /**
- * Constructs a TabPanel.
- * @param tabsPosition a constant indicating TOP or BOTTOM
- * @param tabsStyle a constant indicating ROUNDED or SQUARE
- */
- public TabPanel(int tabsPostion, int tabsStyle)
- {
- super(tabsPostion, tabsStyle);
- vPanels = new Vector();
- String sOS = System.getProperty("os.name");
- if (sOS.equals("Windows 95"))
- bOsHack = true;
- else
- bOsHack = false;
- }
-
- /**
- * Adds a user panel which will be shown when a tab is activated.
- * @param sLabel the tab label
- * @param bEnabled enable the tab or not
- * @param panel the panel to associate with the tab
- * @return the zero-relative index of the newly added tab panel
- */
- public int addTabPanel(String sLabel, boolean bEnabled, Component panel)
- {
- vPanels.addElement(panel);
-
- return addTab(sLabel, bEnabled);
- }
-
- /**
- * Returns the zero-relative index of the currently active panel.
- * @return the currently shown panel or -1 if none are shown
- * @see setCurrentPanelNdx
- */
- public int getCurrentPanelNdx()
- {
- return curIndex;
- }
-
- /**
- * Shows the specified panel.
- * @param index the zero-relative index of the panel to show.
- * @see getCurrentPanelNdx
- */
- public void setCurrentPanelNdx(int index) {
- showTabPanel(index);
-
- // If we aren't designing, set current index even if the panel hasn't been
- // added yet (we'll switch to it in add())
- if (! symantec.beans.Beans.isDesignTime())
- curIndex = index;
- }
-
- /**
- * This standard Java AWT Container method adds a Component to
- * this container. It is overriden here to ensure the Component
- * is added to the end.
- */
- public Component add(Component comp) { return add(comp,-1); }
-
- private String createDefaultLabel(int i) {
- String name="tab - ";
- name += String.valueOf(i);
- return name;
- }
-
- /**
- * This standard Java AWT Container method adds a component to this
- * Container at the given component index.
- * @param comp component to add
- * @param pos zero-relative index at which to add the component or -1 for end
- * @return added component
- * @see #remove
- */
- public synchronized Component add(Component comp, int pos) {
- int newIndex = addTabPanel(createDefaultLabel(vPanels.size()),true,comp);
-
- // If this is the panel that we've set to be the default, or we're designing,
- // go ahead and switch to the new panel.
- if (newIndex == curIndex || symantec.beans.Beans.isDesignTime())
- showTabPanel(newIndex);
- updatePanelLabels();
- return comp;
- }
-
- /**
- * This standard Java AWT method is NOT USED.
- * @param s component name for the LayoutManager
- * @param c component to add to panel
- * @return component parameter
- * @see #remove
- */
- public synchronized Component add(String name, Component comp) { return comp; }
-
- /**
- * Remove a component from this panel.
- * @param comp component to remove from panel
- * @see #add
- */
- public synchronized void remove(Component comp)
- {
- int i=getPanelTabIndex(comp);
-
- if (countTabs()==1)
- removeAllTabPanels();
- else {
- if (i==0)
- showTabPanel(1);
- else
- showTabPanel(i-1);
- removeTabPanel(i);
- }
- }
-
- /**
- * Sets the tab labels associated with the current panels.
- * @param sLabels an array of tab labels for the current panels
- * @see getPanelLabels
- */
- public void setPanelLabels(String[] sLabels)
- {
- labels=sLabels;
- updatePanelLabels();
- }
-
- /**
- * Gets the current tab labels associated with the current panels.
- * @return an array of tab labels for the current panels
- * @see setPanelLabels
- */
- public String[] getPanelLabels()
- {
- return labels;
- }
-
- /**
- * This routine re-sets all the tab labels using the latest String array
- * provided in setPanelLabels().
- * @see setPanelLabels
- */
- public void updatePanelLabels()
- {
- try {
- for (int i=0;i<vPanels.size();i++) {
- String newlabel;
- if (labels!=null) {
- try {
- newlabel=labels[i];
- } catch (ArrayIndexOutOfBoundsException e){
- newlabel=createDefaultLabel(i);
- }
- }else
- newlabel=createDefaultLabel(i);
- setLabel(newlabel,i);
- }
- } catch(Throwable thr) {}
- }
-
- /**
- * Puts the tabs on the top or bottom of the dialog.
- * @param bTabsOnBottom if true the tabs are placed at the bottom of the
- * dialog, if false on top
- * @see getTabsOnBottom
- */
- public void setTabsOnBottom(boolean bTabsOnBottom)
- {
- setTabsInfo(bTabsOnBottom ? BOTTOM : TOP, bTabsOnBottom ? SQUARE : ROUNDED);
- layout();
- }
-
- /**
- * Gets whether the tabs are at the bottom of the dialog.
- * @return true if the tabs are at the bottom of the dialog, false if
- * they are at the top
- * @see setTabsOnBottom
- */
- public boolean getTabsOnBottom()
- {
- return getTabsPosition()==TOP ? false : true;
- }
-
-
- /**
- * Replaces the user panel at the index specified.
- * Allows ability to change tab label and/or tab panel.
- * If it is desired to only change the label, use the base class
- * BaseTabbedPanel's method setTab(String sLabel, boolean bEnabled, int index).
- * @param sLabel the new tab label
- * @param bEnabled enable the tab or not
- * @param panel the new panel
- * @see BaseTabbedPanel#setTab
- * @see getTabPanel
- */
- public synchronized void setTabPanel(String sLabel, boolean bEnabled, Component panel, int index)
- {
- if ((index < 0) || (index >= vPanels.size()))
- return;
-
- if (index == currentTabIndex() && !bEnabled)
- return;
-
- try
- {
- vPanels.setElementAt(panel, index);
- setTab(sLabel, bEnabled, index);
- }
- catch (ArrayIndexOutOfBoundsException e) {}
- }
-
- /**
- * Gets the Panel for the tab at the given index.
- * @param index zero-relative index of the tab to show
- * @return returns the Panel associated with the tab
- * @see setTabPanel
- */
- public synchronized Component getTabPanel(int index)
- {
- if ((index < 0) || (index >= vPanels.size()))
- return null;
-
- Component p = null;
- try
- {
- p = (Component)vPanels.elementAt(index);
- }
- catch (ArrayIndexOutOfBoundsException e) {}
-
- return p;
- }
-
- /**
- * Gets the index for a specific Panel.
- * @param panel the current panel to get the index of
- * @return the zero-relative index of the panel or -1 if it is not found
- */
- public synchronized int getPanelTabIndex(Component panel)
- {
- return vPanels.indexOf(panel);
- }
-
- /**
- * Shows and activates a tab and its tab panel at the given index.
- * The tab position must be enabled.
- * @param index zero-relative index of the tab to show
- * @see enableTabPanel
- */
- public synchronized void showTabPanel(int index)
- {
- if ( tabIsEnabled(index) )
- {
- try
- {
- Component p = (Component) vPanels.elementAt(index);
- showTab(index);
- // if (bOsHack && p != null)
- // {
- // p.hide();
- // setPanel(p);
- // p.show();
- // }
- // else
- showPanel(p);
- }
- catch (ArrayIndexOutOfBoundsException e) {}
- }
- }
-
- /**
- * Enables (or disables) a tab and its tab panel at the given index.
- * The currently active tab cannot be disabled.
- * @param bEnable true to enable, false to disable
- * @param index zero-relative index of the tab
- */
- public synchronized void enableTabPanel(boolean bEnable, int index)
- {
- if ((index < 0) || (index >= vPanels.size()) || index == curIndex)
- return;
-
- enableTab(bEnable, index);
- }
-
- /**
- * Removes a tab and its tab panel at the given index.
- * The current active tab cannot be removed.
- * @param index zero-relative index of the tab
- */
- public synchronized void removeTabPanel(int index)
- {
- if ((index < 0) || (index >= vPanels.size()) || index == curIndex)
- return;
-
- try
- {
- Component p = (Component) vPanels.elementAt(index);
- super.remove(p);
- vPanels.removeElementAt(index);
- }
- catch (ArrayIndexOutOfBoundsException e) {}
-
- removeTab(index);
- }
-
- /**
- * Removes all tab panels, clearing the TabPanel entirely.
- */
- public synchronized void removeAllTabPanels()
- {
- vPanels = new Vector();
- curIndex = -1;
- removeAllTabs();
- }
-
- /**
- * Gets the number of tabs in the TabPanel.
- * @return the number of tabs currently in the TabPanel
- */
- public int countTabs()
- {
- return vPanels.size();
- }
-
- /**
- * This standard routine is called by the Java AWT to handle events.
- * @param evt the event to handle
- * @return true if the event was handled an no further action is needed,
- * false to pass the event to this component's parent
- */
- public boolean handleEvent(Event evt)
- {
- switch (evt.id)
- {
- case Event.ACTION_EVENT:
- if (evt.target instanceof TabPanel)
- {
- if (evt.target == this)
- showTabPanel(currentTabIndex());
- }
- break;
- }
- return super.handleEvent(evt);
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- */
- public Dimension preferredSize()
- {
- Component pan = null;
- Dimension d = null;
- Dimension p = size();
- int s = vPanels.size();
-
- Insets insets = insets();
- p.width -= (insets.left + insets.right);
- p.height -= (insets.top + insets.bottom);
-
- if (p.width < 0)
- p.width = 0;
-
- if (p.height < 0)
- p.height = 0;
-
- for (int x = 0; x < s; x++)
- {
- pan = (Component) vPanels.elementAt(x);
- if (pan != null)
- {
- d = pan.minimumSize();
- if (d.width > p.width)
- p.width = d.width;
- if (d.height > p.height)
- p.height = d.height;
- d = pan.preferredSize();
- if (d.width > p.width)
- p.width = d.width;
- if (d.height > p.height)
- p.height = d.height;
- }
- }
-
- p.width += (insets.left + insets.right);
- p.height += (insets.top + insets.bottom);
-
- return p;
- }
-
- /**
- * Returns the minimum dimensions to properly display this component.
- */
- public Dimension minimumSize()
- {
- Component pan = null;
- Dimension d = null;
- Dimension m = new Dimension(0,0);
- int s = vPanels.size();
-
- for (int x = 0; x < s; x++)
- {
- pan = (Component) vPanels.elementAt(x);
- if (pan != null)
- {
- d = pan.minimumSize();
- if (d.width > m.width)
- m.width = d.width;
- if (d.height > m.height)
- m.height = d.height;
- }
- }
-
- Insets insets = insets();
- m.width += (insets.left + insets.right);
- m.height += (insets.top + insets.bottom);
-
- return m;
- }
- }
-